package com.aitemv.web.index;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aitemv.services.AdminAccService;
import com.aitemv.web.base.BaseAction;

/**
 * 后台登录
 * 
 * @author lyvee
 */
@Controller
@RequestMapping(value = "/back/*")
public class BackIndexAction extends BaseAction {

	@Autowired
	private AdminAccService adminAccService;

	@Override
	@RequestMapping(value = "index.htm", method = RequestMethod.GET)
	public String forward(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		return "back/index";
	}

	/** *ajax方式后台登录*** */
	@RequestMapping(value = "login.htm", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> login(@RequestParam
	String account, @RequestParam
	String password, HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		boolean result = false;
		if (StringUtils.isNotEmpty(account) && StringUtils.isNotEmpty(password)) {
			result = adminAccService.login(account, password);
		} else {
			modelMap.put("success", "false");
		}
		if (result) {
			HttpSession session = request.getSession(false);// 获得session
			if(null== session)//如果session为空
				session = request.getSession(true);
			session.setAttribute("adminAcc", account);
			modelMap.put("success", "true");
		} else {
			modelMap.put("success", "false");
			modelMap.put("msg", "密码或者账号输入错误");
		}
		return modelMap;
	}

	@RequestMapping(value = "main.htm", method = RequestMethod.GET)
	public String main(ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		return "back/main";
	}

}
